/*Hdip Project 2013-14, Student Name:- Chris Pembroke,  Student Number:- x00115839*/
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Collections.Generic;

using System.Web.Mvc;                   // needed namespace  for ActionResult
using System.Web;
using System.Web.Routing;
using System.Security.Principal;

using Exercise1Project.Controllers;  // needed namespaces from project
using Exercise1Project.Models;
using UnitTestBikes.Models;
using Moq;

namespace UnitTestBike.Controllers
{
    [TestClass]
    public class UnitTestBike  // tests the bike controller actions for the return RedirectToAction; return View; and TempData valid
    {
        
        private Mock<IBusinessLogicService> _service;
        private IList<Bike> bikes;

        [TestInitialize]
         public void Intitalize()
        {
            _service = new Mock<IBusinessLogicService>();

            bikes = new List<Bike>()
            {
                new Bike() {BikeID = 1, Make = "Cannondale", Model = "Thrift", Color = "Metalic Green", Frame_Number = "", CustomerID = "abcd"},
                new Bike() {BikeID = 2, Make = "Cube", Model = "Swift", Color = "Blue/Red", Frame_Number = "KL098", CustomerID = "efghi"}
            };
        }
        
        // Bike Controller test methods

        [TestMethod]
        public void Test_Index_returnsView()  // test if bike controller index method returns an Index view when all is well
        {
            //Arrange
            Bike bike = new Bike();
            bike.CustomerID = "abcd";
            _service.Setup(s => s.GetBikeByCustomerID(bike.CustomerID)).Returns(bikes);
             BikeController controller = new BikeController(_service.Object);
             controller.TempData["CustomerId"] = "abcd";   //Note Bene, Note Bene  - very important lesson on this one
            
            //Act
             var result = (ViewResult)controller.Index();

             //Assert
             Assert.AreEqual("Index", result.ViewName);
        }

        [TestMethod]
        public void Test_Index_returnsTypeOfBike()  // test if bike controller index method returns a type of Bike List to index view when all is well
        {
            //Arrange
            Bike bike = new Bike();
            bike.CustomerID = "abcd";
            _service.Setup(s => s.GetBikeByCustomerID(bike.CustomerID)).Returns(bikes);
            BikeController controller = new BikeController(_service.Object);
            controller.TempData["CustomerId"] = "abcd";   //Note Bene, Note Bene  - very important lesson on this one

            //Act
            var result = (ViewResult)controller.Index();
            var model = result.ViewData.Model as IList<Bike>;

            //Assert
            Assert.IsNotNull(model);
            Assert.IsInstanceOfType(model, typeof(IEnumerable<Bike>));
        }

        [TestMethod]
        public void Test_CreateGet_returnsView()
        {
            //Arrange 
            BikeController controller = new BikeController();

            //Act
            var result = (ViewResult)controller.Create();

            //Assert
            Assert.AreEqual("Create", result.ViewName);
        }

        [TestMethod]
        public void Test_CreatePost_redirectToAction()
        {
            //Arrange
            Bike bike = new Bike();
            _service.Setup(s => s.AddCustomerBike(bike)).Returns(true);
            BikeController controller = new BikeController(_service.Object);

            //Act
            var result = (RedirectToRouteResult)controller.Create(bike);

            //Assert
            Assert.AreEqual("Index", result.RouteValues["action"]);
        }

        [TestMethod]
        public void Test_CreatePost_returnsView()
        {
            //Arrange
            Bike bike = new Bike();
            _service.Setup(s => s.AddCustomerBike(bike)).Returns(false);
            BikeController controller = new BikeController(_service.Object);

            //Act
            var result = (ViewResult)controller.Create(bike);

            //Assert
            Assert.AreEqual("Create", result.ViewName);
        }

        [TestMethod]
        public void Test_EditGet_returnsView()
        {
                        //Arrange
            Bike bike = new Bike();
            bike.BikeID = 3;
            _service.Setup(s => s.GetBikeByBikeId(bike.BikeID)).Returns(bike);
             BikeController controller = new BikeController(_service.Object);
             
            //Act
            var result = (ViewResult)controller.Edit(bike.BikeID);

             //Assert
             Assert.AreEqual("Edit", result.ViewName);
        }

        [TestMethod]
        public void Test_EditGet_returnTypeOf()
        {
            //Arrange
            Bike bike = new Bike();
            bike.BikeID = 3;
            _service.Setup(s => s.GetBikeByBikeId(bike.BikeID)).Returns(bike);
            BikeController controller = new BikeController(_service.Object);

            //Act
            var result = (ViewResult)controller.Edit(bike.BikeID);
            var model = result.ViewData.Model as Bike;

            //Assert
            Assert.IsNotNull(model);
            Assert.IsInstanceOfType(model, typeof(Bike));
        }

        [TestMethod]
        public void Test_EditPost_redirectToAction()
        {
            //Arrange
            Bike bike = new Bike();
            _service.Setup(s => s.UpdateBikeDetail(bike)).Returns(true);
            BikeController controller = new BikeController(_service.Object);

            //Act
            var result = (RedirectToRouteResult)controller.Edit(bike);

            //Assert
            Assert.AreEqual("Index", result.RouteValues["action"]);
        }

        [TestMethod]
        public void Test_EditPost_returnsView()
        {
            //Arrange
            Bike bike = new Bike();
            _service.Setup(s => s.UpdateBikeDetail(bike)).Returns(false);
            BikeController controller = new BikeController(_service.Object);

            //Act
            var result = (ViewResult)controller.Edit(bike);

            //Assert
            Assert.AreEqual("Edit", result.ViewName);
        }

        [TestMethod]
        public void Test_EditPost_returnsTypeOf()
        {
            //Arrange
            Bike bike = new Bike();
            _service.Setup(s => s.UpdateBikeDetail(bike)).Returns(false);
            BikeController controller = new BikeController(_service.Object);

            //Act
            var result = (ViewResult)controller.Edit(bike);
            var model = result.ViewData.Model as Bike;

            //Assert
            Assert.IsNotNull(model);
            Assert.IsInstanceOfType(model, typeof(Bike));
        }

        [TestMethod]
        public void Test_DeleteGet_returnsView()
        {
            //Arrange
            Bike bike = new Bike();
            bike.BikeID = 3;
            _service.Setup(s => s.GetBikeByBikeId(bike.BikeID)).Returns(bike);
            BikeController controller = new BikeController(_service.Object);

            //Act
            var result = (ViewResult)controller.Delete(bike.BikeID);

            //Assert
            Assert.AreEqual("Delete", result.ViewName);
        }

        [TestMethod]
        public void Test_DeleteGet_returnTypeOf()
        {
            //Arrange
            Bike bike = new Bike();
            bike.BikeID = 3;
            _service.Setup(s => s.GetBikeByBikeId(bike.BikeID)).Returns(bike);
            BikeController controller = new BikeController(_service.Object);

            //Act
            var result = (ViewResult)controller.Delete(bike.BikeID);
            var model = result.ViewData.Model as Bike;

            //Assert
            Assert.IsNotNull(model);
            Assert.IsInstanceOfType(model, typeof(Bike));
        }

        [TestMethod]
        public void Test_DeletePost_redirectToAction()
        {
            //Arrange
            Bike bike = new Bike();
            FormCollection formCollection = new FormCollection();

            _service.Setup(s => s.GetBikeByBikeId(bike.BikeID)).Returns(bike);
            _service.Setup(s => s.RemoveCustomerBike(bike.BikeID)).Returns(true);
            BikeController controller = new BikeController(_service.Object);

            //Act
            var result = (RedirectToRouteResult)controller.Delete(bike.BikeID, formCollection);

            //Assert
            Assert.AreEqual("Index", result.RouteValues["action"]);
        }

        [TestMethod]
        public void Test_DeletePost_returnsView()
        {
            //Arrange
            Bike bike = new Bike();
            FormCollection formCollection = new FormCollection();

            _service.Setup(s => s.GetBikeByBikeId(bike.BikeID)).Returns(bike);
            _service.Setup(s => s.RemoveCustomerBike(bike.BikeID)).Returns(false);
            BikeController controller = new BikeController(_service.Object);

            //Act
            var result = (ViewResult)controller.Delete(bike.BikeID, formCollection);

            //Assert
            Assert.AreEqual("Delete", result.ViewName);
        }

        [TestMethod]
        public void Test_BikeServices_redirectToAction()
        {
            //Arrange
            Bike bike = new Bike();
            bike.BikeID = 33;
            _service.Setup(s => s.GetBikeByBikeId(bike.BikeID)).Returns(bike);
            BikeController controller = new BikeController(_service.Object);

            //Act
            var result = (RedirectToRouteResult)controller.BikeService(bike.BikeID);

            //Assert
            Assert.AreEqual("ServiceList", result.RouteValues["action"]);

        }

        /*   TESTING TEMPDATA TO BE DONE 
        [TestMethod]
        public void Test_BikeServices_TempDataValues()  
        {
            //Arrange
            Bike bike = new Bike();
            int bike_id = bike.BikeID = 23;
            string bike_make = bike.Make = "Raleigh";
            string bike_model = bike.Model = "Ascender";
            string bike_color = bike.Color = "Red/Green";
            _service.Setup(s => s.GetBikeByBikeId(bike.BikeID)).Returns(bike);
            BikeController controller = new BikeController(_service.Object);

            //Act
            var result = (RedirectToRouteResult)controller.BikeService(bike.BikeID);

            //Assert 
            Assert.AreEqual(bike_id, result.TempData["bike_id"]);
        }

        */




    } // close of class TestClass

} // close of namespace UnitTestBike.Controllers
